package com.os.util;

import java.io.UnsupportedEncodingException;
import java.util.List;

import com.os.model.File;
import com.os.model.Folder;

/**
* @author ZZH-LDX
* @version: 1.0
* 工具类
*/
public class FATUtil {
	
	public static final String ICO = "images/ico.png";
	public static final String FOLDER_IMG = "images/folder.png";
//	public static final String FILE_IMG = "images/file.png";

	public static final String FILE_TXT_IMG = "images/file-txt.png";

	public static final String FILE_IMAGE_JPG = "images/file-jpg.png";
	public static final String FILE_IMAGE_PNG = "images/file-png.png";
	public static final String FILE_IMAGE_DOC = "images/file-doc.png";
	public static final String FILE_IMAGE_DOCX = "images/file-docx.png";
	public static final String FILE_IMAGE_PDF = "images/file-pdf.png";
	public static final String FILE_IMAGE_XLSX = "images/file-xlsx.png";

	public static final String DEFAULT_IMG = "images/file.png";

	public static final String DISK_IMG = "images/disk.png";
	public static final String TREE_NODE_IMG = "images/node.png";
	public static final String FORWARD_IMG = "images/forward.png";
	public static final String BACK_IMG = "images/back.png";
	public static final String SAVE_IMG = "images/save.png";
	public static final String CLOSE_IMG = "images/close.png";
	
	public static final int END = 255; // 文件的磁盘块结束标志!

	public static final int ERROR = -1;//error block
	public static final int FREE = 0;//free block
	
	public static final String DISK = "磁盘";
	public static final String FOLDER = "文件夹";
	public static final String FILE = "文件";
	public static final String EMPTY = "空";
			
	public static final int FLAGREAD = 0;
	public static final int FLAGWRITE = 1;

	/**
	 * 磁盘块大小
	 * @param length
	 * @return
	 */
	public static int blocksCount(int length){
		if (length <= 64){
			return 1;
		} else {
			int n = 0;
			if (length % 64 == 0){
				n = length / 64;
			} else {
				n = length / 64;
				n++;
			}
			return n;
		}
	}

	/**
	 * 文件内容str 转换为字节数
	 * @param str
	 */
	public static int blocksCount(String str) throws UnsupportedEncodingException {
		byte[] bytes = str.getBytes("UTF-8");
		System.out.println("字符串占用字节数： " + bytes.length);
		return bytes.length;
	}


	
	public static double getSize(int length) {
		//length 字节单位 转换为KB 单位
		return Double.parseDouble((String.format("%.2f", length / 1024.0)));
	}
	
//	public static double getFolderSize(Folder folder) {
//		List<Object> children = folder.getChildren();
//		int length = 0;
//		for (Object child : children) {
//			if (child instanceof File) {
//				length += ((File)child).getContent().length();
//			} else {
//				length += getFolderLength((Folder)child);
//			}			
//		}
//		return getSize(length);
//	}
//	
//	public static int getFolderLength(Folder folder) {
//		List<Object> children = folder.getChildren();
//		int totalLength = 0;
//		for (Object child : children) {
//			if (child instanceof File) {
//				totalLength += ((File)child).getContent().length();
//			} else {
//				totalLength += getFolderLength((Folder)child);
//			}			
//		}
//		System.out.println("total" + totalLength);
//		return totalLength;
//	}
	
	public static double getFolderSize(Folder folder) {
		List<Object> children = folder.getChildren();
		double size = 0;
		for (Object child : children) {
			if (child instanceof File) {
				size += ((File)child).getSize();
			} else {
				size += getFolderSize((Folder)child);
			}			
		}
		return Double.parseDouble((String.format("%.2f", size)));
	}
	
}
